home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / strx221.zip / STRSEARC.CPP < prev   
Text File  |  1993-05-19  |  4KB  |  170 lines

  1. //
  2. // strsearch.cpp: str class member functions for searching and replacing
  3. // Author       : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. //
  10. // Version 2.2      5/5/93
  11. //    Fix bug in index relating to case sensitivity (backwards)
  12. //
  13. // Version 2.00     11/31/92
  14. //    Support searching/replacing, regular expressions, case sensitivity
  15. //
  16.  
  17. #include <string.h>
  18. #include <limits.h>
  19.  
  20. #include "str.h"
  21. #include "regx.h"
  22.  
  23.  
  24. // 
  25. // index member functions
  26. //
  27. int str::index(const char * s, int start) const{
  28.    if (!caseSensitive()){
  29.       str mylowstr(::lowercase(*this));
  30.       const char * charptr = strstr(mylowstr(start), ::lowercase(s));
  31.       if (charptr) return (charptr - mylowstr());
  32.       else return (-1);
  33.    }
  34.    else {
  35.       const char * charptr = strstr((*this)(start), s);
  36.       if (charptr) return (charptr - data->buf);
  37.       else return (-1);
  38.    }
  39. };
  40.  
  41. int str::index(const regX& reg, int start) const{
  42.    int matchlen;
  43.    return reg.index(*this, &matchlen, start, caseSensitive());
  44. };
  45.  
  46.  
  47. int str::index(const regX& reg, int * matchLen, int start) const
  48. {
  49.    return reg.index(*this, matchLen, start, caseSensitive());
  50. };
  51.  
  52.  
  53. //
  54. // search operator functions
  55. //
  56.  
  57. int str::search(const char * s, int* startPtr) const{
  58.    *startPtr = index(s, *startPtr);
  59.    return *startPtr>=0;
  60. };
  61.  
  62. int str::search(const char * s, int start) const{
  63.    return index(s, start)>=0;
  64. };
  65.  
  66. int str::search(const regX& reg, int * startPtr) const{
  67.    return search(reg, 0, startPtr);
  68. };
  69.  
  70. int str::search(const regX& reg, int start) const{
  71.    return search(reg, 0, &start);
  72. };
  73.  
  74. int str::search(const regX& reg, str * matchPtr, int start) const{
  75.    return search(reg, matchPtr, &start);
  76. };
  77.  
  78. int str::search(const regX& reg, str * matchPtr, int * startPtr) const{
  79.  
  80.    int matchLen;
  81.    int start = reg.index(*this, &matchLen, *startPtr, caseSensitive());
  82.    if (start>=0) {
  83.       if (matchPtr) *matchPtr = operator()(start, matchLen);
  84.       if (startPtr) *startPtr = start;
  85.       return 1;
  86.    }
  87.    else {
  88.       if (matchPtr) *matchPtr = "";
  89.       if (startPtr) *startPtr = -1;
  90.       return 0;
  91.    };
  92.  
  93. };
  94.  
  95.  
  96. int str::replace(const regX& reg, const char* replaceStr,
  97.                  int start, int numReplacements)
  98. {
  99.   return replace(reg, replaceStr, &start, numReplacements);
  100. };
  101.  
  102. int str::replace(const regX& reg, const char* replaceStr,
  103.                  int* startPtr, int numReplacements)
  104. {
  105.    int& start = *startPtr;
  106.    if (numReplacements==0) return 0;
  107.  
  108.    int countReplacements=0, matchLen;
  109.    int replaceLen = strlen(replaceStr);
  110.  
  111.    do {
  112.       start = index(reg, &matchLen, start);
  113.       if (start<0) break;
  114.       countReplacements++;
  115.       operator()(start, matchLen) = replaceStr;
  116.       start+= replaceLen;           //skip past newly added data
  117.  
  118.       if (!--numReplacements) break;
  119.    } while (1);
  120.  
  121.    return countReplacements;
  122.  
  123. };
  124.  
  125.  
  126.  
  127. int str::replace(const char * searchStr, const char* replaceStr,
  128.                  int start, int numReplacements)
  129. {
  130.   return replace(searchStr, replaceStr, &start, numReplacements);
  131. };
  132.  
  133. int str::replace(const char * searchStr, const char* replaceStr,
  134.                  int* startPtr, int numReplacements)
  135. {
  136.  
  137.    if (numReplacements==0) return 0;
  138.  
  139.    int& start = *startPtr;
  140.    int countReplacements=0;
  141.    int searchLen = strlen(searchStr), replaceLen = strlen(replaceStr);
  142.  
  143.    do {
  144.  
  145.       start = index(searchStr, start);
  146.       if (start<0) break;
  147.       countReplacements++;
  148.       operator()(start, searchLen) = replaceStr;
  149.       start+= replaceLen;           //skip past newly added data
  150.  
  151.       if (!--numReplacements) break;
  152.  
  153.    } while (1);
  154.  
  155.    return countReplacements;
  156.  
  157. };
  158.  
  159.  
  160. int str::replaceAll(const regX& reg, const char* replaceStr, int start)
  161. {
  162.   return replace(reg, replaceStr, &start, INT_MAX);
  163. };
  164.  
  165. int str::replaceAll(const char * searchStr, const char* replaceStr, int start)
  166. {
  167.   return replace(searchStr, replaceStr, &start, INT_MAX);
  168. };
  169.  
  170.